package com.eye.utils;

import com.google.common.collect.Lists;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Properties;

/**
 * Created by rich1 on 10/6/17.
 */
public class SSHUtils{
    /**
     * 链接服务器
     * @param host  主机ip
     * @param port  主机端口
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws JSchException
     */
    public static Session connect(String host, Integer port, String username, String password) throws JSchException{
        JSch jSch = new JSch();
        String cmd = "ls";
        Session session = jSch.getSession(username, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        int timeout = 6000000;
        session.setTimeout(timeout);
        session.connect();
        return session;
    }

    /**
     *
     * @param session 链接的会话
     * @param cmd   需要执的命令
     * @return 输出内容以分好分割
     */
    public static List<String> exec(Session session,String cmd) {
        List<String> result = null;
        ChannelExec channelExec = null;
        InputStream in = null;
        BufferedReader reader =null;
        try{
            if (null == session || StringUtils.isEmpty(cmd)){
                return result;
            }
            channelExec = (ChannelExec) session.openChannel("exec");
            channelExec.setInputStream(null);
            channelExec.setErrStream(System.err);
            channelExec.setCommand(cmd);
            channelExec.connect();

            in = channelExec.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in,Charset.forName("UTF-8")));
            String buf;
            result = Lists.newArrayList();
            while ((buf = reader.readLine()) != null){
                result.add(buf);
            }
        }catch (Exception e){

        }finally{
            if(null != channelExec && channelExec.isConnected()){
                channelExec.disconnect();
            }

            if (null != in){
                try{
                    in.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

            if (null != reader){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 关闭打开的服务器
     * @param session
     */
    public static void sessionClose(Session session){
        if(null != session && session.isConnected()){
            session.disconnect();
        }
    }

}
